﻿
using Kylin.Wiki.IService;
using Kylin.Wiki.Model.Api;
using Kylin.Wiki.Model.Dto;
using Kylin.Wiki.Model.View;
using Kylin.Wiki.Web.Common;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Kylin.Wiki.Web.Controllers.System
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : BaseController
    {
        private readonly IUserService _userService;
        
        public UserController(IUserService userService)
        {
            _userService  = userService;
        }

        [HttpGet("GetUserInfo")]
        public ApiResult<UserInfoVM> GetUserInfo()
        {
            return _userService.GetUserInfo(UserContext.Current.UserId);
        }

        [HttpGet("GetList")]
        public ApiPageResult<List<UserVM>> GetList(UserDTO userDTO)
        {
            return _userService.GetList(userDTO);
        }

        [HttpPost("Create")]
        public ApiResult<bool> Create([FromBody]CreateUserDTO createUserDTO)
        {
            createUserDTO.CreateBy = UserContext.Current.UserName;
            createUserDTO.UpdateBy = UserContext.Current.UserName;
            return _userService.CreateUser(createUserDTO);
        }

        [HttpPost("Update")]
        public ApiResult<bool> Update([FromBody]UpdateUserDTO updateUserDTO)
        {
            updateUserDTO.UpdateBy = UserContext.Current.UserName;
            updateUserDTO.UpdateOn = DateTime.Now;
            return _userService.UpdateUser(updateUserDTO);
        }


        [HttpDelete("{id}")]
        public ApiResult<bool> Delete(int id)
        {
            ApiResult<bool> apiResult = new ApiResult<bool>();
            var user = _userService.FindFirst(x => x.UserID == id);
            if (user == null)
            {
                apiResult.Success = false;
                apiResult.Code = Core.Enum.HttpStateCode.ParameterError;
                return apiResult;
            }
            apiResult.Data = _userService.Delete(user,true)>0;
            return apiResult;
        }



    }
}
